﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;
using System;
using Common;
using Common.Tools;

public class PhotonEngine : MonoBehaviour,IPhotonPeerListener
{
    private static PhotonPeer peer;
    public static PhotonPeer Peer
    {
        get
        {
            return peer;
        }
    }

    public static PhotonEngine Instance;
    
    private Dictionary<OperationCode, Request> RequestDict = new Dictionary<OperationCode, Request>();
    private Dictionary<EventCode, BaseEvent> EventDict = new Dictionary<EventCode, BaseEvent>();

    public static string username;

    void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else if(Instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
    }

    void Start()
    {
        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055", "MyGame1");
    }
    
    void Update()
    {
        peer.Service();

        //if(Input.GetMouseButtonDown(0))
        //{
        //    SendRequest();
        //}
    }

    void SendRequest()
    {
        Dictionary<byte, object> data = new Dictionary<byte, object>();
        data.Add(1, 100);
        data.Add(2, "ejroereo积分及价格");

        PhotonEngine.Peer.OpCustom(1, data, true);

        Debug.Log("SendRequest");
    }

    void OnDestroy()
    {
        if (peer != null && peer.PeerState == PeerStateValue.Connected)
        {
            peer.Disconnect();
        }
    }

    
    public void OnEvent(EventData eventData)
    {
        EventCode code = (EventCode)eventData.Code;
        BaseEvent e = DictTool.GetValue<EventCode, BaseEvent>(EventDict, code);
        e.OnEvent(eventData);

        //switch (eventData.Code)
        //{
        //    case 1:
        //        Debug.Log("收到服务端发送过来的事件");
        //        Dictionary<byte, object> data = eventData.Parameters;
        //        object intValue;
        //        object stringValue;
        //        data.TryGetValue(1, out intValue);
        //        data.TryGetValue(2, out stringValue);
        //        Debug.Log(intValue.ToString());
        //        Debug.Log(stringValue.ToString());
        //        break;
        //    default:
        //        break;
        //}
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        OperationCode opCode = (OperationCode)operationResponse.OperationCode;
        Request request = null;
        bool temp = RequestDict.TryGetValue(opCode, out request);

        if (temp)
        {
            request.OnOperationResponse(operationResponse);
        }
        else
        {
            Debug.Log("没有找到对应的响应处理对象");
        }
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
        Debug.Log(statusCode);
    }

    public void DebugReturn(DebugLevel level, string message)
    {

    }

    public void AddRequest(Request request)
    {
        RequestDict.Add(request.OpCode, request);
    }

    public void RemoveRequest(Request request)
    {
        RequestDict.Remove(request.OpCode);
    }

    public void AddEvent(BaseEvent e)
    {
        EventDict.Add(e.EventCode, e);
    }

    public void RemoveEvent(BaseEvent e)
    {
        EventDict.Remove(e.EventCode);
    }
}
